package view
{
	import model.DataProxy;
	import model.ViewProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String 				= "ApplicationMediator";
		
		public function ApplicationMediator( viewComponent:Object = null )
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			addInitComponents();
		}
		
		override public function listNotificationInterests():Array
		{
			return [ ProgressView.READY, ApplicationFacade.DATA_READY, ApplicationFacade.INIT ];
		}
		
		override public function handleNotification( notification:INotification ):void
		{
			var name:String	= notification.getName();
			var body:Object = notification.getBody();
			
			switch( name )
			{
				case ProgressView.READY:
					sendNotification( ApplicationFacade.DATA_GET );
					break;
				case ApplicationFacade.DATA_READY:
					buildApplication();
					break;
				case ApplicationFacade.INIT:
					sendNotification( MenuView.SHOW );
					sendNotification( LoginView.SHOW );
					sendNotification( ContentView.SHOW );
					viewComponent.init();
					break;
			}
		}
		
		private function addInitComponents():void
		{
			facade.registerMediator( new HeaderViewMediator( viewComponent ) );
			facade.registerMediator( new LoginViewMediator( viewComponent ) );
			facade.registerMediator( new FooterViewMediator( viewComponent ) );
			facade.registerMediator( new ProgressViewMediator( viewComponent ) );
		}
		
		private function buildApplication():void
		{
			facade.registerMediator( new ContentViewMediator( viewComponent ) );
			facade.registerMediator( new MenuViewMediator( viewComponent ) );
			facade.registerMediator( new PopupViewMediator( viewComponent ) );
			
			viewProxy.alignGUI();
		}
		
		public function get dataProxy():DataProxy
		{
			return facade.retrieveProxy( DataProxy.NAME ) as DataProxy;
		}
		
		public function get viewProxy():ViewProxy
		{
			return facade.retrieveProxy( ViewProxy.NAME ) as ViewProxy;
		}
	}
}